package com.atxiao.demo.service;

import com.atxiao.demo.bean.Account;
import com.atxiao.demo.bean.FlowLog;
import com.atxiao.demo.mapper.AccountMapper;
import com.atxiao.demo.mapper.FlowLogMapper;
import com.atxiao.demo.search.FlowLogSearch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author biHuGang
 * @date 2021/3/15
 */
@Service
public class FlowLogServiceImpl implements FlowLogService {

    @Resource
    private FlowLogMapper flowLogMapper;

    @Autowired
    private AccountServiceImpl accountService;

    @Override
    public List<FlowLog> selectFlowLog(Integer card) {
        return flowLogMapper.selectFlowLog(card);
    }

    @Override
    public List<FlowLog> selectFlowLogBySearch(FlowLogSearch search,String idCard) {
        QueryWrapper<FlowLog> wrapper = new QueryWrapper<>();
        if (Strings.isNotBlank(idCard)){
            List<Account> accounts = accountService.getAccountsByUser(idCard);
            List cards = new ArrayList<String>();
            System.out.println("point");
            accounts.forEach(account -> cards.add(account.getCardNum()));
            wrapper.in("card_num",cards);
        }
        if (search != null) {
            //卡号
            if (Strings.isNotBlank(search.getCardNum())) {
                wrapper.like("card_num",search.getCardNum());
            }
            //流水金额
//            if (search.getPriceStart() != null) {
//
//            }
//            if (search.getPriceEnd() != null) {
//
//            }
            //交易时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
            if (Strings.isNotBlank(search.getBusinessTimeStart())) {
                try {
                    Date startDate = simpleDateFormat.parse(search.getBusinessTimeStart());
                    wrapper.ge("business_time",search.getBusinessTimeStart());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if (Strings.isNotBlank(search.getBusinessTimeEnd())) {
                try {
                    Date endDate = simpleDateFormat.parse(search.getBusinessTimeEnd());
                    wrapper.le("business_time",search.getBusinessTimeEnd());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            //存取标志
            if (search.getStatus() != null) {
                wrapper.eq("status",search.getStatus());
            }
            //交易类型
            if (Strings.isNotBlank(search.getBusinessType())) {
                wrapper.eq("business_type",search.getBusinessType());
            }
            //交易结果
//            if (search.getResult() != null) {
//
//            }
        }
        wrapper.orderByDesc("business_time");
        return flowLogMapper.selectList(wrapper);
    }

    @Override
    public List<String> selectTypes() {
        return flowLogMapper.getTypes();
    }

//    private List<Account> getAccountsByUser(String userId){
//        QueryWrapper<Account> wrapper = new QueryWrapper<>();
//        wrapper.eq("id_card",userId);
//        return accountMapper.selectList(wrapper);
//    }
    //    @Override
//    public String selectCard(String card) {
//        return mapper.selectCard(card);
//    }
//
//    @Override
//    public String selectName(String idcard) {
//        return mapper.selectName(idcard);
//    }
}
